package com.ztf0337.mycurriculumdesign.ui.manager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.ztf0337.mycurriculumdesign.R;
import com.ztf0337.mycurriculumdesign.Utils.TextDate;
import com.ztf0337.mycurriculumdesign.entity.ParkLot;
import com.ztf0337.mycurriculumdesign.entity.ParkingSpace;
import com.ztf0337.mycurriculumdesign.service.ParkLotService;
import com.ztf0337.mycurriculumdesign.service.ParkingSpaceService;
import com.ztf0337.mycurriculumdesign.ui.activity.LoginActivity;
import com.ztf0337.mycurriculumdesign.ui.fragment.OrderFragment;

public class ManagerHomeActivity extends AppCompatActivity {

    private ParkLotService parkLotService;
    private ParkingSpaceService parkingSpaceService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.m_activity_home);
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        parkLotService = new ParkLotService(this);
        parkingSpaceService = new ParkingSpaceService(this);

//        for (ParkLot p : TextDate.getParkLots()) {
//            parkLotService.inset(p);
//        }
//        for (ParkingSpace p : TextDate.getParkingSpaces()) {
//            parkingSpaceService.insert(p);
//        }
        ImageView btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(v -> {
            Intent intent = new Intent(this, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            intent.putExtra("online",true);
            this.startActivity(intent);
        });

        Intent intent = getIntent();
        if (intent != null) {
            int open = intent.getIntExtra("open", 1);
            switch (open) {
                case 1 :
                    replaceFragment(new MParkLotFragment());
                    bottomNavigationView.setSelectedItemId(R.id.nav_home);
                    break;
                case 2 :
                    replaceFragment(new MOrderFragment());
                    bottomNavigationView.setSelectedItemId(R.id.nav_order);
                    break;
                case 3 :
                    replaceFragment(new MUserFragment());
                    bottomNavigationView.setSelectedItemId(R.id.nav_user);
                    break;
                default :
                    Log.d("错误","位置返回打开界面值");
            }

        }

        bottomNavigationView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.nav_home) {
                replaceFragment(new MParkLotFragment());
                return true;
            } else if (itemId == R.id.nav_order) {
                replaceFragment(new MOrderFragment());
                return true;
            } else if (itemId == R.id.nav_user) {
                replaceFragment(new MUserFragment());
                return true;
            }
            return false;
        });

    }

    private void replaceFragment(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.content_frame, fragment)
                .commit();
    }

}
